/**
 * 
 */
package com.xbang.attendance.web.action.report.purchaseexecute;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.order.DeliveryDetails;
import com.xbang.attendance.entity.order.PurchaseOrder;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 2886204759816132683L;

	@Autowired
	private SystemService systemService;

	private List<PurchaseOrder> purchaseOrders = new ArrayList<PurchaseOrder>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	@Action("/report/purchaseexecute/list")
	public String list() throws Exception {

		String hqlSentence = "FROM PurchaseOrder purchaseorder WHERE 1 = 1";

		Result result = systemService.searchByPage(hqlSentence, searchComponent, page);
		purchaseOrders = result.getItems();
		page = result.getPage();

		if (purchaseOrders == null || purchaseOrders.isEmpty()) {
			return "index";
		}

		Set<Integer> purchaseIds = new HashSet<Integer>();
		for (PurchaseOrder purchaseOrder : purchaseOrders) {
			purchaseIds.add(purchaseOrder.getId());
		}
		// //////////////
		String detailHqlSentence = "FROM DeliveryDetails details WHERE 1 = 1 ";
		SearchComponent tmpSearchComponent = new SearchComponent();
		tmpSearchComponent.addSearchElement("in.details.deliveryOrder.purchaseOrder.id", purchaseIds);
		List<DeliveryDetails> details = systemService.searchForList(detailHqlSentence, tmpSearchComponent);

		for (DeliveryDetails detail : details) {
			for (PurchaseOrder purchaseOrder : purchaseOrders) {
				if (detail.getDeliveryOrder().getPurchaseOrder().getId().equals(purchaseOrder.getId())) {

					if (detail.getDeliveryOrder().isManualGeneric()) {
						BigDecimal manualGenericAmount = purchaseOrder.getManualGenericDeliveryNumber() == null ? new BigDecimal(0) : purchaseOrder
								.getManualGenericDeliveryNumber();
						manualGenericAmount = manualGenericAmount.add(detail.getProductNumber() == null ? new BigDecimal(0) : detail.getProductNumber());
						purchaseOrder.setManualGenericDeliveryNumber(manualGenericAmount);
					} else {
						BigDecimal autoAmount = purchaseOrder.getAutoDeliveryNumber() == null ? new BigDecimal(0) : purchaseOrder.getAutoDeliveryNumber();
						autoAmount = autoAmount.add(detail.getProductNumber() == null ? new BigDecimal(0) : detail.getProductNumber());
						purchaseOrder.setAutoDeliveryNumber(autoAmount);
					}
				}
			}
		}

		return "index";
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	/**
	 * @return the purchaseOrders
	 */
	public List<PurchaseOrder> getPurchaseOrders() {
		return purchaseOrders;
	}

	/**
	 * @param purchaseOrders
	 *            the purchaseOrders to set
	 */
	public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) {
		this.purchaseOrders = purchaseOrders;
	}

}
